package com.dh.user.controller;

import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dh.user.VO.ResultVO;
import com.dh.user.constant.CookieConstant;
import com.dh.user.constant.RedisConstant;
import com.dh.user.entity.UserInfo;
import com.dh.user.enums.ResultEnum;
import com.dh.user.enums.RoleEnum;
import com.dh.user.service.IRedisService;
import com.dh.user.service.UserInfoService;
import com.dh.user.utils.CookieUtil;
import com.dh.user.utils.ResultVOUtil;


/**
 * @author 作者 dinghao
 * @version 创建时间：2018年11月20日 下午4:20:33
 * @Description 类说明 :
 */
@RestController
@RequestMapping("/login")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private IRedisService iRedisService;

	/**
	 * 
	 * @Title: buyer
	 * @author dinghao
	 * @Description: 买家登录
	 * @return ResultVO
	 */
	@GetMapping("/buyer")
	public ResultVO<Object> buyer(@RequestParam("openId") String openId, HttpServletResponse response) {
		// 1.openid和数据库的数据匹配
		UserInfo userInfo = userInfoService.findByOpenId(openId);
		if (userInfo == null) {
			return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
		}
		// 2.判断角色
		if (RoleEnum.BUYER.getCode() != userInfo.getRole()) {
			return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
		}
		// 3.设置Cookie
		CookieUtil.set(response, CookieConstant.OPENID, openId, CookieConstant.expire);
		return ResultVOUtil.success();
	}

	/**
	 * 
	 * @Title: buyer
	 * @author dinghao
	 * @Description: 卖家登录
	 * @return ResultVO
	 */
	@GetMapping("/seller")
	public ResultVO<Object> seller(@RequestParam("openId") String openId,
			HttpServletRequest request,
			HttpServletResponse response) {
		//判断是否已登录
		Cookie cookie = CookieUtil.get(request,CookieConstant.TOKEN);
		if(cookie != null && 
				!StringUtils.isEmpty((String)iRedisService.get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue())))) {
			return ResultVOUtil.success();
		}
		// 1.openid和数据库的数据匹配
		UserInfo userInfo = userInfoService.findByOpenId(openId);
		if (userInfo == null) {
			return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
		}
		// 2.判断角色
		if (RoleEnum.SELLER.getCode() != userInfo.getRole()) {
			return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
		}
		// 3. redis设置key=UUID,value=xyz
		String token = UUID.randomUUID().toString();
		Long expire = CookieConstant.expire.longValue();
		iRedisService.set(String.format(RedisConstant.TOKEN_TEMPLATE, token),
				openId,expire);
		// 4.设置Cookie
		CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.expire);
		
		return ResultVOUtil.success();
	}

}
